﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for TaiKhoan
/// </summary>
public class TaiKhoan
{
    SqlCommand cmd;
    SqlDataReader dr;

	public TaiKhoan()
	{
		//
		
		//
	}

    public ObjTaiKhoan logIn(ObjTaiKhoan objTaiKhoan)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_TaiKhoanLogin", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[2];
        sqla[0] = new SqlParameter("User_name", DbType.String);
        sqla[1] = new SqlParameter("Pass", DbType.String);

        sqla[0].Value = objTaiKhoan.User_name;
        sqla[1].Value = objTaiKhoan.Pass;

        cmd.Parameters.AddRange(sqla);
        dr = cmd.ExecuteReader();
        ObjTaiKhoan taiKhoan = new ObjTaiKhoan();
        if (dr.Read())
        {
            
            taiKhoan.User_id = int.Parse(dr["user_id"].ToString());
            taiKhoan.User_name = dr["user_name"].ToString();
        }

        dr.Close();
        sqlConnection.Close();
        return taiKhoan;
    }

    public ObjTaiKhoan getTaiKhoan()
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_TaiKhoanGetByAdmin", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        dr = cmd.ExecuteReader();
        ObjTaiKhoan taiKhoan = new ObjTaiKhoan();
        if (dr.Read())
        {

            taiKhoan.User_id = int.Parse(dr["user_id"].ToString());
            taiKhoan.User_name = dr["user_name"].ToString();
            taiKhoan.Pass = dr["Pass"].ToString();
        }

        dr.Close();
        sqlConnection.Close();
        return taiKhoan;
    }

    public void updateTaiKhoan(ObjTaiKhoan objtaikhoan)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_TaiKhoanUpdate", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("pass", DbType.String);
        sqla[0].Value = objtaikhoan.Pass;
        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
    }
}